package com.lanlengran.musicplayer.services;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.BitmapDrawable;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.RemoteViews;

import com.lanlengran.musicplayer.R;
import com.lanlengran.musicplayer.activity.MainActivity;
import com.lanlengran.musicplayer.utils.Utils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dobest on 2016/6/3.
 */
public class PlayService extends Service implements MediaPlayer.OnCompletionListener{
    private List<File> mMusicList=new ArrayList<>();
    private MediaPlayer mMediaPlayer=new MediaPlayer();
    private int mPlayPosition;
    private View mNoticeView;
    private RemoteViews remoteView;
    private boolean isSetListener=false;
    private static PlayService playService;
    private static boolean isStart;
    private boolean isMusicChange =false;
    private String mMusicTitle,mMusicArtist;
    private BitmapDrawable mMusicIcon;
    private Notification notification=null;
    private int NOTICE_BUTTON_REQUEST_CODE=1;
    private IntentFilter intentFilter;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Utils.log_i("onBind");
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Utils.log_i("Service====onCreate");

        intentFilter=new IntentFilter();
        intentFilter.addAction("com.lanlengran.musicplayer.NOTICE_BUTTON_LAST");
        intentFilter.addAction("com.lanlengran.musicplayer.NOTICE_BUTTON_NEXT");
        intentFilter.addAction("com.lanlengran.musicplayer.NOTICE_BUTTON_PAUSE");
        NoticeButtonReceiver noticeButtonReceiver=new NoticeButtonReceiver();
        registerReceiver(noticeButtonReceiver,intentFilter);
        initNotification();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Utils.log_i("onDestroy");
        stopForeground(true);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Utils.log_i("onStartCommand");
        return super.onStartCommand(intent, flags, startId);

    }
    private  void initNotification(){
        Utils.log_i("initNotification");
        Intent intent=new Intent(this, MainActivity.class);
        PendingIntent pendingIntent=PendingIntent.getActivity(this,1,intent,PendingIntent.FLAG_CANCEL_CURRENT);
        Notification.Builder builder=new Notification.Builder(this);
        builder.setContentIntent(pendingIntent);
        mNoticeView=View.inflate(this,R.layout.notification_music_layout,null);
        remoteView=new RemoteViews(this.getPackageName(),R.layout.notification_music_layout);
      //  builder.setContent(remoteView);
        builder.setSmallIcon(R.mipmap.logo);
        notification=builder.build();
        notification.contentView=remoteView;
        //id不能设置为0 否则会导致不能设置为前台服务


        Intent intentPause = new Intent("com.lanlengran.musicplayer.NOTICE_BUTTON_PAUSE");
        PendingIntent pIntentPause = PendingIntent.getBroadcast(this, NOTICE_BUTTON_REQUEST_CODE,
                intentPause, 0);
        remoteView.setOnClickPendingIntent(R.id.notice_pause, pIntentPause);

        Intent lastIntenet=new Intent("com.lanlengran.musicplayer.NOTICE_BUTTON_LAST");
        PendingIntent lastPendingIntent=PendingIntent.getBroadcast(this,NOTICE_BUTTON_REQUEST_CODE,
                lastIntenet,0);
        remoteView.setOnClickPendingIntent(R.id.notice_last,lastPendingIntent);

        Intent nextIntenet=new Intent("com.lanlengran.musicplayer.NOTICE_BUTTON_NEXT");
        PendingIntent nextPendingIntent=PendingIntent.getBroadcast(this,NOTICE_BUTTON_REQUEST_CODE,
                nextIntenet,0);
        remoteView.setOnClickPendingIntent(R.id.notice_next,nextPendingIntent);
        startForeground(1,notification);

    }
    public void onPlay(int position){
        Utils.log_i("准备播放");
        mPlayPosition=position;
        playMusic(position);
        if (!isSetListener){
            mMediaPlayer.setOnCompletionListener(this);
            isSetListener=true;
        }

    }
    public int getPlayPosition(){
        return mPlayPosition;
    }
    public void onStop(){
        mMediaPlayer.stop();
    }
    public void onStart(){
        mMediaPlayer.start();
    }
    public void  onPause(){
        mMediaPlayer.pause();
    }
    private void playMusic(int position) {
        try {
            mMediaPlayer.reset();
            //     i++;
            mMediaPlayer.setDataSource(mMusicList.get(position).getPath());
            mMediaPlayer.prepare();
            mMediaPlayer.start();
            Utils.log_i("开始播放");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void nextMusic() {
        mPlayPosition++;
        if (mPlayPosition>=mMusicList.size()){
            mPlayPosition=0;
        }
        playMusic(mPlayPosition);
        Log.i("qin:","当前播放"+mPlayPosition);
    }
    private void lastMusic(){
        mPlayPosition--;
        if (mPlayPosition<0){
            mPlayPosition=mMusicList.size()-1;
        }
        playMusic(mPlayPosition);
        Log.i("qin:","当前播放"+mPlayPosition);
    }
    public void setmMusicList(List<File> mMusicList) {
        this.mMusicList = mMusicList;
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        Utils.log_i("下一首");
        nextMusic();
    }
    public static PlayService getPlayService(){
        return playService;
    }

    public static void setPlayService(PlayService playService) {
        PlayService.playService = playService;
    }

    public static boolean getIsStart() {
        return isStart;
    }

    public static void setIsStart(boolean isStart) {
        PlayService.isStart = isStart;
    }

    public void setNoticeInfo(String title,String artist,BitmapDrawable mMusicIcon){
            mMusicTitle=title;
            mMusicArtist=artist;
            mMusicIcon=mMusicIcon;

    }

    public void setMusicChange(boolean musicChange) {
        isMusicChange = musicChange;
    }
    class NoticeButtonReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            Utils.log_i("收到广播"+intent.getAction());
            switch (intent.getAction()){
                case "com.lanlengran.musicplayer.NOTICE_BUTTON_PAUSE":
                    onPause();
                    break;
                case "com.lanlengran.musicplayer.NOTICE_BUTTON_LAST":
                    lastMusic();
                    break;
                case "com.lanlengran.musicplayer.NOTICE_BUTTON_NEXT":
                    nextMusic();
                    break;
            }
        }
    }
}
